package com.cb.domain.service.Insurance.Impl;

import java.text.ParseException;
import java.util.Date;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import webservices.bizHangYi.ArrayOfCOMIOContactPerson;
import webservices.bizHangYi.COMIOContactPerson;
import webservices.bizHangYi.COMIOVisitor;
import webservices.bizHangYi.INSIOCancelPolicyInfo;
import webservices.bizHangYi.INSIOOrderGenerateResult;
import webservices.bizHangYi.INSIOOrderGenerateSimpleRequest;
import webservices.bizHangYi.IOContactPersonType;
import webservices.bizHangYi.IOIDType;
import webservices.bizHangYi.IOOrdIDPrvdThdPrtType;
import webservices.bizHangYi.NewBizHangYiSoap;
import webservices.bizHangYi.TravelskyBaseResult;

import com.cb.common.webService.ClientService;
import com.cb.domain.model.order.BOrder;
import com.cb.domain.model.order.Flight;
import com.cb.domain.model.order.Passenger;
import com.cb.domain.service.Insurance.InsuranceSaver;


@Service("newBizHybx")
public class NewBizHybx implements InsuranceSaver {

	 protected final Log log = LogFactory.getLog(getClass());
	@Override
	public String saveInsuranceByBOrder(BOrder order, Passenger passenger) {
		// TODO Auto-generated method stub
		
		
		ClientService<NewBizHangYiSoap> clientService = new ClientService<NewBizHangYiSoap>();
		NewBizHangYiSoap service = clientService.getClient("newBizHybx.path",NewBizHangYiSoap.class);
		
		
		Set<Flight> flights= order.getFlights();
		Flight flight=null;
		for (Flight ft : flights) {
			flight=ft;
			break;
		}
		INSIOOrderGenerateSimpleRequest simpleRequest=new INSIOOrderGenerateSimpleRequest();
		simpleRequest.setUserTotalPrice("20");
		simpleRequest.setNumber(1);
		
		simpleRequest.setInsStarttime(flight.getDepDay());
		
		Date date;
		try {
			date = com.cb.core.util.DateUtil.convertStringToDate("yyyy-MM-dd hh:mm:ss", flight.getDepDay()+" "+flight.getDepTime()+":00");
			simpleRequest.setInsEndttime(com.cb.core.util.DateUtil.convertDateToString(com.cb.core.util.DateUtil.addDate(date,7)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.equals(e);
		}
		simpleRequest.setFightNumber(flight.getCode());
		simpleRequest.setFightDate(flight.getDepDay()+" "+flight.getDepTime()+":00");
		simpleRequest.setInsType(2);
		simpleRequest.setInsPrdType(IOOrdIDPrvdThdPrtType.TAI_PING);
		simpleRequest.setInsNumber(order.getOrderNo());
		simpleRequest.setInsProductType("tpszx");
		ArrayOfCOMIOContactPerson person=new ArrayOfCOMIOContactPerson();
		COMIOContactPerson contactPerson=new COMIOContactPerson();

		contactPerson.setContactName(passenger.getName());
		contactPerson.setContactPhone(order.getContactMob());
		contactPerson.setGender("男");
		contactPerson.setContactbirthday(cn.cloud.utils.DateUtil.getInstance().formatDate2(passenger.getBirthday()));
		contactPerson.setContactPersonType(IOContactPersonType.CUSTOMER);
		contactPerson.setIdentityCardNo(passenger.getCardNum());
		contactPerson.setIdType(IOIDType.IDENTITY);
		person.getCOMIOContactPerson().add(contactPerson);
		simpleRequest.setPersonInfos(person);

		COMIOVisitor visitor=initVisitor();
		INSIOOrderGenerateResult generateResult= service.insIFOrderGenerateSimple(visitor,simpleRequest);
		int i=1;
		return generateResult.getOrderId();
	}

	@Override
	public String cancleInsuranceByBOrder(String orderId, Date date) {
		
		ClientService<NewBizHangYiSoap> clientService = new ClientService<NewBizHangYiSoap>();
		NewBizHangYiSoap service = clientService.getClient("newBizHybx.path",NewBizHangYiSoap.class);
		COMIOVisitor visitor=initVisitor();
		INSIOCancelPolicyInfo cancelPolicyInfo=new INSIOCancelPolicyInfo();
		cancelPolicyInfo.setInsOrderID(orderId);
		TravelskyBaseResult baseResult= service.insIFCancelPolicySimple(visitor, cancelPolicyInfo);
		return baseResult.getTravelskyResult();
	}
	
	public static void main(String [] args){
		NewBizHybx bizHybx=new NewBizHybx();
		bizHybx.cancleInsuranceByBOrder("62011110220120048820", new Date());
	}
	
	private COMIOVisitor initVisitor(){
		COMIOVisitor visitor=new COMIOVisitor();
		visitor.setVisitorID("gdsundm");
		visitor.setUserID("wzbx01-01");
		visitor.setProviderID("wzbx01");
		visitor.setIdentityCheck("GbkGOZZxQVDRG3A2ff7/6g==");
		
		return visitor;
	}
}
